import { ObjectType } from './../../types/obj-type';
import { Injectable } from '@nestjs/common';
import NodeAuth from 'node-auth0';
import { v4 as uuidv4 } from 'uuid';
import { create } from 'svg-captcha';

@Injectable()
export class ToolsService {
  private nodeAuth: NodeAuth;
  constructor() {
    this.nodeAuth = new NodeAuth();
  }
  /**
   * @Description: 生成uuid的方法
   * @param {type}
   * @return:
   */
  public get uuid(): string {
    return uuidv4();
  }
  /**
   * @Description: 生产图形验证码的方法
   * @param {type}
   * @return:
   */
  getCaptcha(): { data: any, text: string } {
    const codeConfig = {
      size: 4,// 验证码长度
      ignoreChars: '0o1i', // 验证码字符中排除 0o1i
      noise: 2, // 干扰线条的数量
      height: 40,
      width: 100,
      color: true,
      background: '#f0f0f0'
    };
    const captcha = create(codeConfig);
    return captcha;
  }

  /**
   * @Description: 密码加密的方法
   * @param {type}
   * @return:
   */
  makePassword(password: string): string {
    return this.nodeAuth.makePassword(password);
  }

  /**
   * @Description: 校验密码加密
   * @param {type}
   * @return:
   */
  checkPassword(password: string, sqlPassword: string): boolean {
    return this.nodeAuth.checkPassword(password, sqlPassword);
  }
  /**
     * @Description: 递归整理父子关系数据
     * @return:
     */
  arrayToTree(data: ObjectType[], pid: number): ObjectType[] {
    const temp = [];
    const treeArr = data;
    treeArr.forEach((item, index) => {
      if (item.pid == pid) {
        if (this.arrayToTree(treeArr, treeArr[index].id).length > 0) {
          treeArr[index].children = this.arrayToTree(treeArr, treeArr[index].id);
        }
        temp.push(treeArr[index])
      }
    })
    return temp;
  }

}

